import os
import Joomla
import MySQLdb
import JclicgUtils

class Component:
    def __init__(self, dir, name):
        self._utils = JclicgUtils.JclicgUtils()
        self._name = name
        self._dir = dir + "/components/com_" + self._name

    def add_file(self, filename):
        fh = open(self._dir + "/" +filename, "w")

    def addTable(self):
        joomla = Joomla.Joomla()

        db_user = joomla.getConfig('user')
        db_password = joomla.getConfig('password')
        db_host = joomla.getConfig('host')
        db_name = joomla.getConfig('db')

        name = raw_input("name:")
        table = raw_input("table:")
        
        conn = MySQLdb.connect(passwd=db_password, user=db_user, host=db_host, db=db_name)

        cur = conn.cursor()
        cur.execute("DESCRIBE " + table)
        rows = cur.fetchall()

        content = "<?\ndefined('_JEXEC') or die('Restricted access');\nclass Table" + name.capitalize() + " extends JTable\n{\n"
        for row in rows:
            content += "\tpublic $" + row[0] + ";\n"
        content += "\n\tfunction __construct(& $db)\n\t{\n\t\tparent::__construct('" + table + "', 'id', $db);\n\t}\n}\n?>"
        
        self._utils.createFile(self._dir + "/tables/" + name + ".php", content)

        self._utils.createFile(self._dir + "/models/" + name + ".php",
                "<?\ndefined('_JEXEC') or die('Restricted access');\njimport('joomla.application.component.model');\n" +
                "class " + self._name.capitalize() + "Model" + name.capitalize() + " extends JModel\n{\n\tfunction findAll()\n\t{\n\t\t" +
                "$query = 'SELECT * FROM " + table + "';\n\t\t$this->_db->setQuery($query);\n\t\treturn $this->_db->loadObjectList();\n\t" +
                "}\n\tfunction findById($id)\n\t{\n\t\t$query = 'SELECT * FROM " + table + " WHERE id=' . $id;\n\t\t$this->_db->setQuery($query);\n\t\t" +
                "return $this->_db->loadObject();\n\t}\n\tfunction store($data)\n\t{\n\t\t$row =& $this->getTable();\n\t\t" +
                "if(!$row->bind($data) || !$row->check() || !$row->store()) {\n\t\t\t$this->setError($this->_db->getErrorMsg());\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n}\n?>")
                
    def create(self):
        self._utils.createDir(self._dir)
        self._utils.createDir(self._dir + "/views")
        self._utils.createDir(self._dir + "/views/" + self._name)
        self._utils.createDir(self._dir + "/views/" + self._name + "/tmpl")
        self._utils.createDir(self._dir + "/tables")
        self._utils.createDir(self._dir + "/models")

        self._utils.createFile(self._dir + "/" + self._name + ".php",
                "<?\ndefined('_JEXEC') or die('Restricted access');\nrequire_once(JPATH_COMPONENT.DS.'controller.php');\n"+
                "if($controller = JRequest::getWord('controller')) {\n\t$path = JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php';\n\t"+
                "if(file_exists($path)) {\n\t\trequire_once $path;\n\t} else {\n\t\t$controller = '';\n\t}\n}\n$classname = '" + self._name + "Controller'.$controller;\n$controller = new $classname();\n" +
                "$controller->execute(JRequest::getWord('task'));\n$controller->redirect();\n?>")
        self._utils.createFile(self._dir + "/controller.php",
                "<?\ndefined('_JEXEC') or die('Restricted access');\njimport('joomla.application.component.controller');\n"+
                "class " + self._name.capitalize() + "Controller extends JController\n{\n\tfunction display() {\n\t\tparent::display();\n\t}\n }\n?>")
        self._utils.createFile(self._dir + "/views/" + self._name + "/view.html.php",
                "<?\ndefined('_JEXEC') or die('Restricted access');\njimport('joomla.application.component.view');\n" +
                "class " + self._name.capitalize() + "View" + self._name.capitalize() + " extends JView\n{\n\tfunction display($tpl = null)\n\t{\n\t\t$model = &$this->getModel();\n\t\tparent::display($tpl);\n\t}\n}\n?>")
        self._utils.createFile(self._dir + "/views/" + self._name + "/tmpl/default.php", "<?\ndefined('_JEXEC') or die('Restricted access');\n?>")
        self._utils.createFile(self._dir + "/models/" + self._name + ".php",
                "<?\ndefined('_JEXEC') or die('Restricted access');\njimport('joomla.application.component.model');\n" +
                "class " + self._name.capitalize() + "Model" + self._name.capitalize() + " extends JModel\n{\n}\n?>")